﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Projekt
{
    public class Statistics
    {
        private static int words = 0;
        private static int characters = 0;
        private static double charactersPerWord = 0.0;

        
        /// <summary>
        /// Method counts words 
        /// </summary>
        /// <param name="webpageData">Webpagedata - Object of data holding class WebpageData</param>
        /// <returns>int - Number of words in text</returns>
        private static int countWords(WebpageData webpageData)
        {
            MatchCollection matches = Regex.Matches(webpageData.PlainText, @"[\S]+");

            return matches.Count;
        }

        
        /// <summary>
        /// Method counts characters
        /// </summary>
        /// <param name="webpageData">Webpagedata - Object of data holding class WebpageData</param>
        /// <returns>int - Number of characters in text</returns>
        private static int countCharacters(WebpageData webpageData)
        {
            return webpageData.PlainText.Length;
        }

        
        /// <summary>
        /// Method show statistic (words and characters)
        /// </summary>
        /// <param name="webpageData">Webpagedata - Object of data holding class WebpageData</param>
        public static void showStatistics(WebpageData webpageData)
        {
            words = countWords(webpageData);
            characters = countCharacters(webpageData);

            if (words != 0)
                charactersPerWord = (double)characters / (double)words;

            string cpw = charactersPerWord.ToString("#,##0.00");

            System.Windows.Forms.MessageBox.Show("Der Text besteht aus " + words + " Wörtern!\nDer Text besteht aus " + characters + " Zeichen!\nEin Wort besitzt im Schnitt aus " + cpw + " Zeichen!", "Information", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
        }
    }
}
